package com.xb.cloud.product.merchant.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.model.PageParam;
import com.xb.cloud.product.merchant.service.client.ShopPromotionSkuService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.client.bo.ShopPromotionSkuBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ShopPromotionSkuInput;
import com.xb.cloud.product.service.business.model.ShopPromotionSku;
import com.xb.cloud.product.service.business.model.ShopPromotionSkuExample;
import com.xb.cloud.product.service.business.model.ShopPromotionSkuExample.Criteria;
import com.xb.cloud.product.service.business.model.mapper.ShopPromotionSkuMapper;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

@Service
public class ShopPromotionSkuServiceImpl extends AbstractService implements ShopPromotionSkuService {

	@Resource
	private ShopPromotionSkuMapper shopPromotionSkuMapper;

	@Override
	public ServiceReturnInfo createShopPromotionSku(ShopPromotionSkuInput shopPromotionSkuInput,
			EndMerchantUserBo endMerchantUserBo) {
		ShopPromotionSku shopPromotionSku = convert(shopPromotionSkuInput, endMerchantUserBo);
		shopPromotionSkuMapper.insertSelective(shopPromotionSku);
		return ServiceReturnUtils.success();
	}

	/**
	 * 
	 * @param shopPromotionSkuList
	 * @param endMerchantUserBo
	 * @return
	 * @see com.xb.cloud.product.merchant.service.client.ShopPromotionSkuService#createShopPromotionSkuList(java.util.List,
	 *      com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo)
	 */
	@Override
	public ServiceReturnInfo createShopPromotionSkuList(List<ShopPromotionSkuInput> shopPromotionSkuInputList,
			EndMerchantUserBo endMerchantUserBo) {
		List<ShopPromotionSku> shopPromotionSkuList = new ArrayList<>();
		for (ShopPromotionSkuInput shopPromotionSkuInput : shopPromotionSkuInputList) {
			ShopPromotionSku shopPromotionSku = convert(shopPromotionSkuInput, endMerchantUserBo);
			shopPromotionSkuList.add(shopPromotionSku);
		}
		// list插入数据库
		shopPromotionSkuMapper.createShopPromotionSkuBatch(shopPromotionSkuList);
		return ServiceReturnUtils.success();
	}

	// 属性设置
	private ShopPromotionSku convert(ShopPromotionSkuInput shopPromotionSkuInput, EndMerchantUserBo endMerchantUserBo) {
		ShopPromotionSku shopPromotionSku = new ShopPromotionSku();
		shopPromotionSku.setId(ipKeyGenerator.generateKey());
		shopPromotionSku.setSkuId(shopPromotionSkuInput.getSkuId());
		shopPromotionSku.setShopId(endMerchantUserBo.getShopId());
		shopPromotionSku.setSort(shopPromotionSkuInput.getSort());
		shopPromotionSku.setIsDeleted(DbConstants.IS_DELETED_FALSE);
		shopPromotionSku.setIsEnabled(DbConstants.TRUE);

		shopPromotionSku.setCreateTime(timeService.getDate());
		shopPromotionSku.setCreateUserId(endMerchantUserBo.getId());
		shopPromotionSku.setCreateUserName(endMerchantUserBo.getLoginName());
		return shopPromotionSku;
	}

	/**
	 * 
	 * @param skuId
	 * @return
	 * @see com.xb.cloud.product.merchant.service.client.ShopPromotionSkuService#getShopPromotionSkuBySkuId(java.lang.Long)
	 */
	@Override
	public ServiceReturnInfo getShopPromotionSkuBySkuId(Long skuId) {
		ShopPromotionSkuExample shopPromotionSkuExample = new ShopPromotionSkuExample();
		Criteria criteria = shopPromotionSkuExample.createCriteria();
		criteria.andSkuIdEqualTo(skuId);
		criteria.andIsDeletedEqualTo(DbConstants.IS_DELETED_FALSE);
		criteria.andIsEnabledEqualTo(DbConstants.TRUE);
		List<ShopPromotionSku> list = shopPromotionSkuMapper.selectByExample(shopPromotionSkuExample);
		if (list.size() < 1) {
			return ServiceReturnUtils.success();
		}
		ShopPromotionSku shopPromotionSku = list.get(0);
		ShopPromotionSkuBo shopPromotionSkuBo = new ShopPromotionSkuBo();
		shopPromotionSkuBo.setSort(shopPromotionSku.getSort());
		list = null;
		return ServiceReturnUtils.success(shopPromotionSkuBo);
	}

	/**
	 * 
	 * @param ids
	 * @return
	 * @see com.xb.cloud.product.merchant.service.client.ShopPromotionSkuService#deleteShopPromotionSkuList(java.lang.Long[])
	 */
	@Override
	public ServiceReturnInfo deleteShopPromotionSkuList(Long[] ids) {
		shopPromotionSkuMapper.deleteShopPromotionSkuBatch(ids);
		return ServiceReturnUtils.success();
	}

	/**
	 * 
	 * @param updateShopPromotionSkuInputList
	 *            批量修改
	 * @param endMerchantUserBo
	 * @return
	 * @see com.xb.cloud.product.merchant.service.client.ShopPromotionSkuService#updateShopPromotionSkuList(java.util.List,
	 *      com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo)
	 */
	@Override
	public ServiceReturnInfo updateShopPromotionSkuList(List<ShopPromotionSkuInput> updateShopPromotionSkuInputList,
			EndMerchantUserBo endMerchantUserBo) {
		ShopPromotionSku shopPromotionSku = null;
		for (ShopPromotionSkuInput shopPromotionSkuInput : updateShopPromotionSkuInputList) {
			shopPromotionSku = shopPromotionSkuMapper.selectByPrimaryKey(shopPromotionSkuInput.getId());
			shopPromotionSku.setSort(shopPromotionSkuInput.getSort());
			shopPromotionSku.setUpdateTime(timeService.getDate());
			shopPromotionSku.setUpdateUserId(endMerchantUserBo.getId());
			shopPromotionSku.setUpdateUserName(endMerchantUserBo.getLoginName());
			shopPromotionSkuMapper.updateByPrimaryKeySelective(shopPromotionSku);
		}
		return ServiceReturnUtils.success();
	}

	/**
	 * 
	 * @param id
	 * @return
	 * @see com.xb.cloud.product.merchant.service.client.ShopPromotionSkuService#deleteShopPromotionSkuList(java.lang.String)
	 */
	@Override
	public ServiceReturnInfo deleteShopPromotionSkuList(String id) {
		shopPromotionSkuMapper.deleteByPrimaryKey(Long.parseLong(id));
		return ServiceReturnUtils.success();
	}

	/**
	 * 
	 * @param shopPromotionSkuBo
	 * @return
	 * @see com.xb.cloud.product.merchant.service.client.ShopPromotionSkuService#getShopPromotionSkuList(com.xb.cloud.product.merchant.service.client.bo.ShopPromotionSkuBo)
	 */
	@Override
	public ServiceReturnInfo getShopPromotionSkuList(ShopPromotionSkuBo shopPromotionSkuBo) {
		ShopPromotionSku shopPromotionSku = new ShopPromotionSku();
		shopPromotionSku.setIsDeleted(DbConstants.IS_DELETED_FALSE);
		shopPromotionSku.setIsEnabled((DbConstants.TRUE));
		shopPromotionSku.setShopId(shopPromotionSkuBo.getShopId());
		PageParam pageParam = PageParam.resetPageParam(shopPromotionSkuBo.getStartPage(),
				shopPromotionSkuBo.getPageSize());

		List<ShopPromotionSkuBo> list = shopPromotionSkuMapper.selectByExamplePaged(shopPromotionSku, pageParam);
		return ServiceReturnUtils.success(list);
	}

	/**   
	 *   
	 * @param shopId
	 * @return   
	 * @see com.xb.cloud.product.merchant.service.client.ShopPromotionSkuService#deleteShopPromotionSkuBatch(java.lang.Long)   
	 */
	@Override
	public ServiceReturnInfo deleteShopPromotionSkuBatch(Long shopId) {
		ShopPromotionSkuExample example = new ShopPromotionSkuExample();
		example.createCriteria().andShopIdEqualTo(shopId);
		shopPromotionSkuMapper.deleteByExample(example);
		return ServiceReturnUtils.success();
	}

}
